package cn.wy.org.service.impl;

import cn.wy.basic.util.BaiduAuditUtils;
import cn.wy.org.dto.ShopDto;
import cn.wy.exception.BusinessException;
import cn.wy.org.domain.Employee;
import cn.wy.org.domain.Shop;
import cn.wy.org.domain.ShopAuditLog;
import cn.wy.org.mapper.EmployeeMapper;
import cn.wy.org.mapper.ShopAuditLogMapper;
import cn.wy.org.mapper.ShopMapper;
import cn.wy.org.service.ShopService;
import cn.wy.basic.service.impl.BaseServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements ShopService {
    /**
     * 业务实现类：
     */
        @Autowired
        private EmployeeMapper employeeMapper;

        @Autowired
        private ShopMapper shopMapper;

        @Autowired
        private ShopAuditLogMapper shopAuditLogMapper;

        @Autowired
        private JavaMailSender javaMailSender;

        @Override
        @Transactional
        public void settlement(Shop shop) {
            //        t_employee
            //        id - 自增长
            //        username - 前端传递
            //        phone - 前端传递
            //        email - 前端传递
            //        salt - 业务里面生成 - 今天不用管，后面注册时候再说
            //        password - 前端传递
            //        age   - 不用管
            //        state -   启用禁用，先禁用
            //        department_id - 所属部门不用管
            //        logininfo_id - 不用管 - 登录的时候再考虑
            //        shop_id -

            //        t_shop
            //        id - 自增长
            //        name - 前端传递
            //        tel - 前端传递
            //        registerTime - 入驻时间/注册时间，默认当前系统时间
            //        state - 状态，默认1
            //        address - 前端传递
            //        logo - 前端传递
            //        admin_id -

            //1.先添加t_employee => 自增长id
            //2.将自增长id设置到shop对象
            //3.再添加t_shop => 自增长id
            //4.将shop的自增长id设置到employee对象
            //5.修改t_employee

            //一：校验：
            //1.空值校验，前端校验容易被禁用掉，需要后端校验
            if(StringUtils.isEmpty(shop.getName())){
                throw new BusinessException("店铺名称不能为空!!!");
            }
            if(StringUtils.isEmpty(shop.getAddress())){
                throw new BusinessException("店铺地址不能为空!!!");
            }

            //2.校验：百度AI智能审核
            //审核文本
            if (!BaiduAuditUtils.TextCensor(shop.getName())){
                throw new BusinessException("店铺名称不合法!!!");
            }
            //审核图像：如果你上传了图片 ，但是上传的图片不合规
            if(!StringUtils.isEmpty(shop.getLogo())
                    && !BaiduAuditUtils.ImgCensor("http://123.207.27.208"+shop.getLogo())){
                throw new BusinessException("店铺LOGO不合法!!!");
            }

            //3.校验当前店铺是否入驻：只要名称和地址是一样的
            Shop dbShop = shopMapper.findByNameAndAddress(shop);
            if(dbShop != null){
                throw new BusinessException("你的店铺已经入驻，请直接登录!如果忘记密码，请联系管理员!!!");
            }

            //二：添加到数据库
            //1.先添加t_employee => 自增长id
            Employee employee = shop.getAdmin();
            employee.setState(0);//先设置为禁用，审核通过并激活成功之后设置为1
            employeeMapper.insert(employee);

            //2.将自增长id设置到shop对象
            shop.setAdminId(employee.getId());
            //3.再添加t_shop => 自增长id
            shopMapper.insert(shop);

            //4.将shop的自增长id设置到employee对象
            employee.setShop_id(shop.getId());
            //5.修改t_employee
            employeeMapper.update(employee);
        }

        @Override
        @Transactional
        public void pass(ShopAuditLog log) throws MessagingException {
            //1.改店铺状态：shop state 1 - 2
            Shop shop = shopMapper.findOne(log.getShopId());
            shop.setState(2);
            shopMapper.update(shop);
            //2.添加审核日志：
            log.setState(2);
            shopAuditLogMapper.insert(log);
            //3.发送邮件：告诉店铺管理员激活邮件使用
            //创建复杂邮件对象
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            //发送复杂邮件的工具类
            //true - 能够添加附件
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("2218180746@qq.com");
            helper.setSubject("店铺激活邮件");
            //true - 能够再内容中编写html标签 - 会解析
            helper.setText("<h1>你的店铺已经注册!!!</h1><img src='http://123.207.27.208"+shop.getLogo()
                            +"' ><a href='http://localhost:8080/shop/active/"+shop.getId()+"'>点击该链接激活</a>",
                    true);
            Employee employee = employeeMapper.findOne(shop.getAdminId());
            //收件人
            helper.setTo(employee.getEmail());
            javaMailSender.send(mimeMessage);
        }

        @Override
        @Transactional
        public void reject(ShopAuditLog log) throws MessagingException {
            //1.改店铺状态：shop state 1 - 4
            Shop shop = shopMapper.findOne(log.getShopId());
            shop.setState(4);
            shopMapper.update(shop);

            //2.添加审核日志：
            log.setState(4);
            shopAuditLogMapper.insert(log);

            //3.发送邮件：告诉店铺管理员激活邮件使用
            //创建复杂邮件对象
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            //发送复杂邮件的工具类
            //true - 能够添加附件
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("2218180746@qq.com");
            helper.setSubject("店铺审核失败");
            //true - 能够再内容中编写html标签 - 会解析
            helper.setText("<h1>你的店铺入驻失败，请修改信息之后重新入驻</h1>",
                    true);
            Employee employee = employeeMapper.findOne(shop.getAdminId());
            //收件人
            helper.setTo(employee.getEmail());
            javaMailSender.send(mimeMessage);
        }

        @Override
        public List<ShopDto> echarts() {
            return shopMapper.echarts();
        }


}
